package com.xx.controller;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

@RestController
public class ConsumerController {

    @Resource
    @Qualifier(value = "restTemplate")
    private RestTemplate restTemplate;

    @Resource
    @Qualifier(value = "restTemplateWithoutLoadBalanced")
    private RestTemplate restTemplateWithoutLoadBalanced;

    /**
     * 通过直连服务进行负载均衡
     *
     * @return String
     */
    @RequestMapping("/info")
    public String info() {
        return restTemplate.getForObject("http://PROVIDER-00/info/", String.class);
    }

    /**
     * 模拟调用zuul网关进行负载均衡；作用跟浏览器地址栏里直接输入：http://127.0.0.1:8007/s2/info/ 一致
     *
     * @return String
     */
    @RequestMapping("/zuul-info")
    public String zuulInfo() {
        String res = restTemplateWithoutLoadBalanced.getForObject("http://127.0.0.1:8007/s2/info/", String.class);
        System.out.println(res);
        return res;
    }
}
